package com.sample.config;

import com.sample.client.KresnaApiClient;
import feign.Feign;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import feign.okhttp.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * Created by leo.na on 21/03/2017.
 */
@Configuration
public class FeignConfig {

    @Bean
    public KresnaApiClient getRootResourceClient(@Value("${server.port}") int serverPort) {
        KresnaApiClient client = Feign.builder()
                .decoder(new JacksonDecoder())
                .encoder(new JacksonEncoder())
                .client(new OkHttpClient(
                        new okhttp3.OkHttpClient.Builder()
                                .connectTimeout(30, TimeUnit.SECONDS)
                                .build()))
                .target(KresnaApiClient.class, "http://localhost:" + serverPort);
        return client;
    }

}
